package com.pqc.tbk.web.money;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pqc.tbk.entity.account.User;
import com.pqc.tbk.entity.money.ConfirmMoneyOut;
import com.pqc.tbk.service.account.ShiroDbRealm.ShiroUser;
import com.pqc.tbk.service.mail.MailService;
import com.pqc.tbk.service.money.ConfirmOutService;
import com.pqc.tbk.utils.MSGSender;
import com.pqc.tbk.utils.MailSender;
import com.pqc.tbk.utils.UserUtil;

/**
 * 财产管理的控制器<br>
 * 
 * @author long, create on 2012-7-4 下午7:39:44
 * 
 */
@Controller
@RequestMapping(value = "/user/home")
public class ConfirmOutController {

	private ConfirmOutService confirmOutService;
	private MailService mailService;

	/**
	 * 跳转到兑现页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/pay_cash")
	public String toPayCash(final ModelMap map) {
		final Long uid = ((ShiroUser) SecurityUtils.getSubject().getPrincipal())
				.getId();
		final User user = confirmOutService.findByUId(uid);
		map.put("user", user);
		return "money/pay_cash";
	}

	@RequestMapping(value = "/apply_money")
	@ResponseBody
	public String applyMoney(final ConfirmMoneyOut out) {
		final boolean result = confirmOutService.applyMoney(UserUtil.getUID(),
				out);
		final String content = UserUtil.getShiroUser().getName() + "提现:"
				+ out.getMoney() + "元";
		MailSender.send(MailSender.ACCEPT_MSG_ADDRESS, "提现申请", content);
		try {
			MSGSender.sendMessage(MSGSender.MANAGER_NUM, content);
		}
		catch (final Exception e) {

		}
		if (result) {
			return "success";
		} else {
			return "failure";
		}
	}

	@Autowired
	public void setMoneyService(final ConfirmOutService confirmOutService) {
		this.confirmOutService = confirmOutService;
	}

}
